<?php

namespace Api\Controller;

use Think\Controller;

class TaskController extends CommonController {

    protected $ladderModel;
    protected $orderModel;
    protected $orderItemModel;
    protected $accountDetailModel;

    function _initialize() {
        parent::_initialize();

        $this->ladderModel = D('ladder');
        $this->orderModel = D('order');
        $this->orderItemModel = D('orderItem');
        $this->accountDetailModel = D('accountDetail');

    }

    //移除过期订单
    public function removeExpireOrders() {

        $sql = "UPDATE __ORDER_ITEM__ oi
                LEFT JOIN __ORDER__ o
                ON oi.order_id = o.order_id
                LEFT JOIN __LADDER__ l
                ON oi.ladder_id = l.ladder_id
                SET l.gaming_status = " .C('LADDER_GAMING_STATUS.FREE'). " 
                WHERE o.status = " .C('ORDER_STATUS.WAITING'). " 
                AND TIMESTAMPDIFF(MINUTE,o.create_time,NOW()) >= 2;";
        

        $sql .= "DELETE o.*,oi.* FROM __ORDER__ o
                 LEFT JOIN __ORDER_ITEM__ oi
                 ON o.order_id = oi.order_id
                 WHERE o.status = ".C('ORDER_STATUS.WAITING'). " 
                 AND TIMESTAMPDIFF(MINUTE,o.create_time,NOW()) >= 2;";

        M()->execute($sql);

    }

    //检查进行中订单是否超过两小时
    public function expireNewOrders() {

        $sql = "UPDATE __ORDER_ITEM__ oi
                LEFT JOIN __ORDER__ o
                ON oi.order_id = o.order_id
                LEFT JOIN __LADDER__ l
                ON oi.ladder_id = l.ladder_id
                SET oi.status = " .C('ORDER_ITEM_STATUS.SUCCESS'). ", 
                oi.end_time = '" .getCurrentDate(). "', 
                o.status = " .C('ORDER_STATUS.END'). ", 
                o.end_time = '" .getCurrentDate(). "', 
                l.gaming_status = " .C('LADDER_GAMING_STATUS.FREE'). ", 
                l.total_assets = l.total_assets + ceil(o.money * " .C('LADDER_ORDER_FEE_PERCENT'). "), 
                l.last_game_end_time = '" .getCurrentDate(). "'
                WHERE oi.status = " .C('ORDER_ITEM_STATUS.NEW'). " 
                AND TIMESTAMPDIFF(MINUTE,oi.begin_time,NOW()) >= 120";

        M()->execute($sql);

    }

    //检查陪练确认时间是否超过3分钟
    public function expireLadderConfirms() {

        $sql = "UPDATE __ORDER_ITEM__ oi
                LEFT JOIN __ORDER__ o
                ON oi.order_id = o.order_id
                LEFT JOIN __LADDER__ l
                ON oi.ladder_id = l.ladder_id
                SET oi.status = " .C('ORDER_ITEM_STATUS.FAIL_HOLD'). ", 
                oi.end_time = '" .getCurrentDate(). "', 
                o.status = " .C('ORDER_STATUS.HOLD'). ",  
                l.gaming_status = " .C('LADDER_GAMING_STATUS.FREE'). ", 
                l.last_game_end_time = '" .getCurrentDate(). "'
                WHERE oi.status IN (3,6) 
                AND TIMESTAMPDIFF(MINUTE,oi.fail_request_time,NOW()) >= 2";

        M()->execute($sql);

    }

    //发送短信提示20分钟已到
    public function sendRemindSms() {

        $where = "o.status = " .C('ORDER_STATUS.NEW').
                 " AND oi.status = " .C('ORDER_ITEM_STATUS.NEW').
                 " AND TIMESTAMPDIFF(MINUTE,oi.begin_time,NOW()) = 20";

        $orderList = $this->orderModel->alias('o')
            ->join('left join __USER__ u ON o.user_id = u.user_id')
            ->join('left join __ORDER_ITEM__ oi ON o.order_id = oi.order_id')
            ->join('left join __LADDER__ l ON oi.ladder_id = l.ladder_id')
            ->join('left join __USER__ uu on l.user_id = uu.user_id')
            ->field('u.mobile as player_mobile, uu.mobile as ladder_mobile')
            ->where($where)
            ->select();

        if(!isNullOrEmpty($orderList)) {
            $playerContent = "游戏结束时请及时对游戏结果进行正确的反馈，以便我们对订单进行实时处理，谢谢！";
            $ladderContent = "游戏结束时请及时提醒老板对游戏结果进行正确的反馈，以便我们对订单进行实时处理，谢谢！";

            foreach ($orderList as $order) {
                //发送给用户
                mms(C('mmskey'), $order['player_mobile'], $playerContent);
                //发送给打手
                mms(C('mmskey'), $order['ladder_mobile'], $ladderContent);
            }

        }

    }

}